<template>
  <base-breadcrumb v-loading="loading">
    <div class="about-master-container">
      <a-card>
        <title-name title="基本信息" />
        <a-form-model>
          <a-row>
            <template
              v-for="item in form.basicInformationVOS"
            >
              <a-col :span="8" v-if="item.type === 0 || item.type === 1 || item.type === 2">
                <a-form-model-item :label="item.name">
                  <BaseInput v-model="item.input" disabled />
                </a-form-model-item>
              </a-col>
            </template>

            <a-col :span="8">
              <a-form-model-item label="填写人">
                <BaseInput v-model="form.creatorName" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="验收日期">
                <BaseInput v-model="form.realTime" disabled />
              </a-form-model-item>
            </a-col>
          </a-row>

        </a-form-model>
      </a-card>

      <a-card v-if="form.checkItemTreeVO">
        <a-tabs :active-key="activeKey" @change="activeKey = $event">
          <a-tab-pane :key="index" :tab="index<1?'安全验收':('复验'+index)"
                      v-for="(item,index) in form.checkItemTreeVO"
          >
            <title-name title="安全验收" />
            <a-table
              bordered
              :columns="columns"
              :data-source="getTableData2([item])"
              :pagination="false"
            >
              <template slot="index" slot-scope="text, record, index">{{ index + 1 }}</template>
            </a-table>

            <div class="mt20"></div>
            <title-name title="验收结论" />
            <a-form-model>
              <a-row>
                <a-col :span="8">
                  <a-form-model-item label="验收结果">
                    <BaseInput disabled :value="form.checkDTOS[index].result?'合格':'不合格'" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="验收结论">
                    <BaseTextarea disabled v-model="form.checkDTOS[index].conclusion" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item label="上传图片">
                    <BUploadVModel
                      disabled
                      uploadType="image-list"
                      v-model="form.checkDTOS[index].imgFileInfoList"
                      businessCode="SHE05"
                    ></BUploadVModel>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="8">
                  <a-form-model-item label="外部人员签字">
                    <a-table
                      bordered
                      :columns="columns1"
                      :data-source="form.checkDTOS[index].outSignDtos"
                      :pagination="false"
                    >
                      <template slot="img" slot-scope="text, record, index">
                        <img :src="text" style="max-width: 350px;" />
                      </template>
                    </a-table>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="8">
                  <a-form-model-item label="总包人员签字">
                    <a-table
                      bordered
                      :columns="columns2"
                      :data-source="form.checkDTOS[index].dutyPersonDdtos"
                      :pagination="false"
                    >
                      <template slot="img" slot-scope="text, record, index">
                        <img :src="text" style="max-width: 350px;" />
                      </template>
                    </a-table>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
          </a-tab-pane>
        </a-tabs>
      </a-card>

      <div class="pub-header-btn page-btn-right-top">
        <BasePrintButton v-if="form.checkItemTreeVO" type="primary" style="display: inline-block"
                         :before-print-fn="printFn"
        />
        <a-button @click="$close">关闭</a-button>
      </div>
    </div>
  </base-breadcrumb>
</template>
<script>
import BasePrintButton from '@/components/BasePrintButton' // 打印按钮
import { STable } from '@/components'
import { detail, getPrintContent } from '@/api/safetySupervision/acceptancePlan'
import BUploadVModel from '@/components/BComponents/BUploadVModel'

export default {
  name: 'a' + Date.now(),
  components: {
    STable,
    BasePrintButton,
    BUploadVModel
  },
  data() {
    return {
      activeKey: 0,
      printFn: () => {
        return getPrintContent({
          id: this.form.id,
          index: this.form.checkDTOS[this.activeKey].treeSortNum
        }).then(res => {
          return res.data?.content
        })
      },
      columns: [
        { title: '序号', width: 70, scopedSlots: { customRender: 'index' } },
        { title: '验收项目', dataIndex: 'type', width: 200, align: 'center' },
        { title: '验收内容', dataIndex: 'content' },
        { title: '验收结果', dataIndex: 'result', width: 250, align: 'center' }
      ],
      columns1: [
        { title: '签字单位', dataIndex: 'orgTypeName', width: 120 },
        { title: '签字', dataIndex: 'signImge', scopedSlots: { customRender: 'img' } }
      ],
      columns2: [
        { title: '责任人', dataIndex: 'userName', width: 120 },
        { title: '签字', dataIndex: 'signImge', scopedSlots: { customRender: 'img' } }
      ],
      form: {
        detailsVO: {},
        checkItemTreeVO: []
      },
      loading: false,
      tableData: []

    }
  },
  created() {
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.getData()
    }
  },
  methods: {
    async getData() {
      this.loading = true
      let res = await detail({ id: this.form.id })
      this.loading = false
      if (res.code === this.SUCCESS_CODE) {
        this.form = res.data
      }
    },
    getTableData(item) {
      let temp = []
      item.children.map(v => {
        if (v.children && v.children.length) {
          v.children.map(w => {
            if (w.children && w.children.length) {
              w.children.map(z => {
                temp.push({
                  type: w.name,
                  content: z.name,
                  result: z.input
                })
              })
            } else {
              temp.push({
                type: v.name,
                content: w.name,
                result: w.input
              })
            }
          })
        } else {
          temp.push({
            type: item.name,
            content: v.name,
            result: v.input
          })
        }
      })
      return temp
    },
    getTableData2(list, parent = {}, data = []) {
      list.map(v => {
        if (v.children && v.children.length) {
          this.getTableData2(v.children, v, data)
        } else {
          data.push({
            type: parent.name,
            content: v.name,
            result: v.input
          })
        }
      })
      return data
    }
  }
}
</script>
